/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com */

/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "textedit.h"



namespace tri {
namespace ui {


extTextEdit::extTextEdit() :
	tri::ui::extLabel(),mTextLengthLimit(1024){
	connect<extTextEdit>(tri::KEY_PRESSED,this,&extTextEdit::_key_event);
	connect<extTextEdit>(tri::FOCUS_LOST,this,&extTextEdit::_apply_text_changes);
	setStyle("DefaultTextEditStyle");
}

extTextEdit::~extTextEdit() {
}

void extTextEdit::setTextSizeLimit(int limit){
	limit = limit>1024?1024:limit;
	limit = limit<0?0:limit;
	mTextLengthLimit = limit;
}

void extTextEdit::_key_event(int key){
	std::string text = getText();
	switch(key){
	case tri::KEY_BACKSPACE:
		if (text!=""){
			std::string newText = text.substr(0,text.length()-1);
			setText(newText.c_str());
		}
		break;
	case tri::KEY_ENTER:
		_apply_text_changes();
		break;
	default:
		if(key >= 32 && key < 127){
			if (text.length()<mTextLengthLimit){
				text.append(1,(char)key);
				setText(text.c_str());
			}				
		}
		break;
	}
}


void extTextEdit::_apply_text_changes(){
	emit(tri::VALUE_CHANGED);
}


void extTextEdit::_revert_text(){
}


}
}

